import requests
from time import time,sleep,strftime
from urllib.parse import urlparse
from random import choice
import re
import aiohttp


USEPROXY = False


def show_log(*args, **kwargs):
    print(strftime('[%y-%m-%d %H:%M:%S] '), *args, **kwargs)

class IP:
    def __init__(self,ip,count=0,max_age=7*60):
        self.ip = ip
        self.count = count
        self.err = False
        self.time = int(time())
        self.max_age = max_age
    
    def __call__(self):
        self.count += 1
        return {"http": self.ip,"https":self.ip}

    def __str__(self):
        return self.ip

class IpPool:
    urls = [
        "http://EDDB49DB385875AB810F0684E6E9EBBF.standard.hutoudaili.com/?num=1000&area_type=1&anonymity=3&order=1&style=1",
    ]

    proxy_index = 0
    ipMaxUse = 10
    def __init__(self):
        self.pool = []
    
    async def fill_pool(self):
        global USEPROXY
        count = 0
        while True:
            show_log("collect ip in ",self.urls[self.proxy_index])
            # r = requests.get(self.urls[self.proxy_index])

            async with aiohttp.ClientSession() as session:
                async with session.get(self.urls[self.proxy_index]) as response:
                    raw_ips = await response.text()

            ips = re.findall(r'(\d+\.\d+\.\d+\.\d+:\d+)', raw_ips)
            show_log(f"get ips {len(ips)}")
            if not ips:
                count += 1
                if count >= 10:
                    self.proxy_index += 1
                    count = 0
                    if self.proxy_index >= len(self.urls):
                        show_log('can not get ip\nset use ip False')
                        USEPROXY = False
                        return False

                sleep(1)
            self.pool.extend(map(lambda ip: IP(ip),ips))
            return True

    async def getIp(self):
        flag = True
        if not self.pool:
            flag = await self.fill_pool()
        if flag:
            while True:
                ip = choice(self.pool)
                old = (int(time()) - ip.time) > ip.max_age
                if ip.count >= self.ipMaxUse or ip.err or old:
                    self.pool.remove(ip)
                    show_log(f"Pool 移除 {ip.ip} count {ip.count} err {ip.err} old {old}")
                    continue
                break
            return ip
        else:
            return None

